// mircoApp/car-manage/carAppeal/carAppeal.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardImage: [],
    popupShow: false,
    radio: "",
    carAppealForm: {
      mobile: "",
      license_no: "",
      source: "",
      idcard_imgs: [],
      driving_permit_imgs: []
    },
    specialForm: {
      car_id: "",
      group_id: "",
      driving_license_imgs: [],
      work_license_imgs: []
    },
    tip_one: "",
    tip_two: "",
    car_list: [],
    page_name: "特殊车辆申请",
    isclick: false,
    source: 0,

    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.name) {
      this.setData({
        name: options.name
      })
    }
    if (options.source) {
      this.setData({
        source: options.source
      })
    }
    if (options.group_id) {
      this.setData({
        group_id: options.group_id
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.name == 'carAppeal_special_apply') {
      this.getGroupDetail();
    } else {
      this.page_name = "车辆申诉";
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  confirmTap() {
    if (this.data.isclick) return;
    this.data.isclick = true;
    // 提交按钮
    if (this.data.name == 'carAppeal_special_apply') {
      console.log('特殊车辆申请')
      // 特殊车辆申请
      this.special_apply();
    } else {
      // 申诉请求
      console.log('申诉请求')
      this.carAppealSave();
    }
  },
  onChange(e) {
    console.log(e)
    this.setData({
      radio: e.detail
    })
  },
  tapSelect_Car() {
    this.getCarList();
  },
  onClick(e) {
    let {
      name
    } = e.currentTarget.dataset;
    this.setData({
      radio: name
    })
  },
  onClickLeft() {
    this.setData({
      popupShow: false,
      radio: '',
      page: 1,
      total_page: 0
    })
  },
  onClickRight() {
    this.setData({
      popupShow: false,
      page: 1,
      total_page: 0
    })
  },
  inp_field(e) {
    let {
      key,
      keys
    } = e.target.dataset;
    console.log(key, keys, e)
    this.setData({
      [`${key}.${keys}`]: e.detail
    })
  },
  async getCarList() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.car-manage.frontend.controllers.special.apply.get-car-list"),
      data: {
        page: this.data.page
      }
    })
    if (data.result) {
      this.setData({
        car_list: data.data.data,
        total_page: data.data.last_page,
        popupShow: true
      })
    } else {
      app.tips(data.msg)
    }
  },
  async getCarListMore() {
    if (this.page >= this.total_page) return;
    this.setData({
      page: this.data.page + 1
    })
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.car-manage.frontend.controllers.special.apply.get-car-list"),
      data: {
        page: this.data.page
      }
    })
    if (data.result) {
      this.setData({
        car_list: this.data.car_list.concat(data.data.data)
      })
    } else {
      app.tips(data.msg)
      this.setData({
        page: this.data.page - 1
      })
    }
  },
  async getGroupDetail() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.car-manage.frontend.controllers.special.apply.group-details"),
      data: {
        group_id: this.data.group_id
      }
    })
    if (data.result) {
      this.setData({
        tip_one: data.data.tip_one,
        tip_two: data.data.tip_two,
        page_name: data.data.group_name
      })
      wx.setNavigationBarTitle({
        title: this.data.page_name,
      })
    } else {
      app.tips(data.msg)
    }
  },
  async special_apply() {
    let json = {
      data: {
        car_id: this.data.radio,
        group_id: this.data.group_id,
        driving_license_imgs: this.data.specialForm.driving_license_imgs,
        work_license_imgs: this.data.specialForm.work_license_imgs
      }
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.car-manage.frontend.controllers.special.apply"),
      data: json
    })
    if (data.result) {
      app.tips(data.msg)
      setTimeout(() => {
        wx.navigateTo({
          url: '/mircoApp/car-manage/special_vehicles/special_vehicles',
        })
        this.data.isclick = false;
      }, 500);
    } else {
      this.data.isclick = false;
      app.tips(data.msg)
    }
  },
  async carAppealSave() {
    let json = {
      data: {
        mobile: this.data.carAppealForm.mobile,
        license_no: this.data.carAppealForm.license_no,
        source: this.data.source,
        idcard_imgs: this.data.carAppealForm.idcard_imgs,
        driving_permit_imgs: this.data.carAppealForm.driving_permit_imgs
      }
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.car-manage.frontend.controllers.appeal.index.appeal"),
      data: json
    })
    if (data.result) {
      app.tips(data.msg);
      setTimeout(() => {
        wx.navigateTo({
          url: '/packageG/member_v2/member_v2',
        })
        this.data.isclick = false;
      }, 500);
    } else {
      this.data.isclick = false;
      app.tips(data.msg)
    }
  },
  delImg(e) {
    let {
      ind,
      key,
      keys
    } = e.target.dataset;
    if (key == "specialForm") {
      this.data.specialForm[keys].splice(ind, 1);
      this.setData({
        [`specialForm.${keys}`]: this.data.specialForm[keys]
      })
    } else {
      this.data.carAppealForm[keys].splice(ind, 1);
      this.setData({
        [`carAppealForm.${keys}`]: this.data.carAppealForm[keys]
      })
      console.log(this.data.carAppealForm)
    }
  },
  afterRead(event) {
    let {
      key,
      keys
    } = event.currentTarget.dataset;
    let {
      file
    } = event.detail;
    console.log('key对应的字段', key, keys)
    console.log(event)
    wx.showLoading({
      title: '上传中',
    })
    if (file && file.length) {
      for (let i = 0; i < file.length; i++) {
        this.onRead_1(file[i], key, keys)
      }
    } else {
      this.onRead_1(file, key, keys)
    }

  },
  onRead_1(file, key, keys) {
    let urlStr = app.getNetAddresss('upload.uploadPic');
    wx.uploadFile({
      url: urlStr,
      filePath: file.url,
      name: 'file',
      formData: null,
      success: (res) => {
        wx.hideLoading();
        let _res = JSON.parse(res.data);
        if (key == "carAppealForm") {
          if (keys == "idcard_imgs") {
            this.data.carAppealForm.idcard_imgs.push(_res.data.img_url);
            this.setData({
              'carAppealForm.idcard_imgs': this.data.carAppealForm.idcard_imgs
            })
          } else {
            this.data.carAppealForm.driving_permit_imgs.push(_res.data.img_url)
            this.setData({
              'carAppealForm.driving_permit_imgs': this.data.carAppealForm.driving_permit_imgs
            })
          }
        } else {
          if (keys == 'driving_license_imgs') {
            this.data.specialForm.driving_license_imgs.push(_res.data.img_url);
            this.setData({
              'specialForm.driving_license_imgs': this.data.specialForm.driving_license_imgs
            })
          } else {
            console.log(this.data.specialForm.work_license_imgs)
            this.data.specialForm.work_license_imgs.push(_res.data.img_url);
            this.setData({
              'specialForm.work_license_imgs': this.data.specialForm.work_license_imgs
            })
          }
        }
      },
      fail: (err) => {
        wx.hideLoading()
      }
    });
  }
})